package com.zxl.working.controller;

import com.zxl.working.entity.admin.SysUser;
import com.zxl.working.entity.backend.SellerInfo;
import com.zxl.working.service.InvoiceService;
import com.zxl.working.utils.CommUtil;
import com.zxl.working.utils.HttpUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@CrossOrigin
@RestController
@RequestMapping("/invoice")
public class InvoiceController {

    @Autowired
    private InvoiceService invoiceService;

    @Resource
    private CommUtil commUtil;

    @Value("${request.url}")
    private String requestUrl;

    @Autowired
    private HttpUtil httpUtil;

    @PostMapping("/qryConsignee")
    public String qryConsignee(HttpServletRequest request,@RequestBody String params){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }
        return invoiceService.qryConsignee(sellerInfo.getSellerId(),params);
    }

    @PostMapping("/qryTaxType")
    public String qryTaxType(){
        return invoiceService.qryTaxType();
    }

    @PostMapping("/qryInvoiceInfo")
    public String qryInvoiceInfo(HttpServletRequest request,@RequestBody String params){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }
        return invoiceService.qryInvoiceInfo(sellerInfo,params);
    }

    @PostMapping("/insSellerBill")
    public String insSellerBill(HttpServletRequest request,@RequestBody String params)
    {
        SysUser sysUser = (SysUser) request.getSession().getAttribute("loginUser");
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        return invoiceService.insSellerBill(sysUser,params,sellerInfo.getSellerName());
    }

    @PostMapping("/qryBills")
    public String qryBills(HttpServletRequest request,@RequestBody String params){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }
        return invoiceService.qryBills(sellerInfo.getSellerId(),params);
    }

    @PostMapping("/qryConsigneeList")
    public String qryConsigneeList(HttpServletRequest request,@RequestBody String params){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        String res = invoiceService.qryConsigneeList(sellerInfo.getSellerId(),params);

        return res;
    }

    @PostMapping("/insConsignee")
    public String insConsignee(HttpServletRequest request,@RequestBody String params){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        invoiceService.insConsignee(sellerInfo.getSellerId(),params);

        return commUtil.returnSuccess("");
    }

    @PostMapping("/updConsignee")
    public String updConsignee(HttpServletRequest request,@RequestBody String params){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        invoiceService.updConsignee(sellerInfo.getSellerId(),params);

        return commUtil.returnSuccess("");
    }

    @PostMapping("/delConsignee")
    public String delConsignee(HttpServletRequest request,@RequestBody String params){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        invoiceService.delConsignee(params);

        return commUtil.returnSuccess("");
    }

    @PostMapping("/qryTax")
    public String qryTax(HttpServletRequest request,@RequestBody String params){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        return invoiceService.qryTax(sellerInfo.getSellerId(),params);
    }

    @PostMapping("/qryAccountDetails")
    public String qryAccountDetails(HttpServletRequest request,@RequestBody String params){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        return invoiceService.qryAccountDetails(sellerInfo.getSellerId(),params);
    }

    @PostMapping("/qryDetailTotal")
    public String qryDetailTotal(HttpServletRequest request){
        SellerInfo sellerInfo = (SellerInfo) request.getSession().getAttribute("sellerInfo");
        if(ObjectUtils.isEmpty(sellerInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        return invoiceService.qryDetailTotal(sellerInfo.getSellerId());
    }

}
